package com.shemy.fileservice.service;

import com.shemy.fileservice.domain.FileEntity;
import com.shemy.fileservice.repository.FileEntityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description
 * @Author Dzsom
 * @Date 2018-10-19 23:18
 * @Encoding UTF-8
 * @Version 1.0
 **/
@Service
public class FileServiceImpl implements FileService {

    private final FileEntityRepository fileRepository;

    @Autowired
    public FileServiceImpl(FileEntityRepository fileRepository) {
        this.fileRepository = fileRepository;
    }

    @Override
    @CachePut(cacheNames = "file",key = "#file.id",cacheManager = "fileRedisCacheManager")
    public FileEntity saveFile(FileEntity file) {

        return fileRepository.save(file);
    }

    @Override
    @CacheEvict(cacheNames = "file",key = "#id",cacheManager = "fileRedisCacheManager")
    public void removeFile(String id) {
        fileRepository.delete(id);

    }

    @Override
    @Cacheable(cacheNames = "file",key = "#id",cacheManager = "fileRedisCacheManager")
    public FileEntity getFileById(String id) {
        return fileRepository.findOne(id);
    }

    @Override
    public List<FileEntity> listFiles() {
        return fileRepository.findAll();
    }
}
